package be.nvlooy.moviereminder.controller;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import be.nvlooy.moviereminder.common.SessionMessage;
import be.nvlooy.moviereminder.entities.User;
import be.nvlooy.moviereminder.security.SecurityFacade;

public class BasicController {
	
	private SecurityFacade securityFacade;
	
	private SessionMessage sessionMessage;
	
	private MessageSource messageSource;
	
	@Autowired
	public void setSecurityFacade(SecurityFacade securityFacade) {
		this.securityFacade = securityFacade;
	}

	@Autowired
	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}
	
	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	public User getAuthenticatedUser() {
		return securityFacade.getAuthenticatedUser();
	}
	
	public boolean isAuthenticated() {
		return securityFacade.isLoggedIn();
	}
	
	public String getMessage(String messageId) {
		return getMessage(messageId, Locale.getDefault());
	}
	
	public String getMessage(String messageId, Locale locale) {
		return messageSource.getMessage(messageId, null, locale);
	}
	
	public String getMessage(String messageId, Object... messageParameters) {
		return getMessage(messageId, Locale.getDefault(), messageParameters);
	}
	
	public String getMessage(String messageId, Locale locale, Object... messageParameters) {
		return messageSource.getMessage(messageId, messageParameters, locale);
	}
	
	public void showFrontendMessage(String content) {
		sessionMessage.setMessage(content);
	}
	
	public void showFrontendMessage(String content, SessionMessage.MessageType messageType) {
		sessionMessage.setMessage(content, messageType);
	}
	
	public void showFrontendMessage(String messageId, Object... messageParameters) {
		showFrontendMessage(getMessage(messageId, 
				messageParameters, 
				Locale.getDefault()));
	}
	
	public void showFrontendMessage(String messageId, 
			SessionMessage.MessageType messageType, 
			Object... messageParameters) {
		showFrontendMessage(getMessage(messageId, messageParameters, Locale.getDefault()), messageType);
	}
}
